package com.foreveross.pushsdk.core;

import android.util.Log;

import com.foreveross.pushsdk.entity.TimeEntity;

import java.util.Date;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @author Huang Li
 * @version 创建时间：2014年5月17日 上午11:20:28
 * 验证发送的3心跳是否被接收，如果没有接收就进行重新连接
 */
public class HeartbeatReciverList {
    /**
     * 心跳发送与接收到的时间间隔，错过这个时间就认为已经错过
     */
	private static final int MAX_HEARTBEAT_INTERVAL=5000;
	private static Queue<TimeEntity> list=new LinkedList<TimeEntity>();
	public static void insert(TimeEntity timeEntity){
		if (list.size()==3) {
			list.poll();
		}
		 list.offer(timeEntity);
	}
	public static void modif(){
			Date dateNow=new Date();
			for (TimeEntity iEntity : list) {
				if(iEntity.isReciver()==false){
					if(dateNow.getTime()
							-iEntity.getCreateTime().getTime()
							<MAX_HEARTBEAT_INTERVAL) {
						Log.i("PushService", "Modif "+iEntity.getCreateTime().getTime()+" to True");
						iEntity.setReciver(true);
						return;
					}
				}
			}	
	}
	public static void clear() {
		list.clear();
	}
	public static boolean isOffline(){
		if(list.size()<3) return false;
		for (TimeEntity iEntity : list) {
			if(iEntity.isReciver()==true) return false;
		}
		return true;
	}
}

